# upload_configs.ps1 (English Prompts Version)

# --- Please modify the following variables ---
$remoteUser = "root"              # Your remote server username
$remoteHost = "192.168.137.100"   # Your remote server IP address
$remoteBaseConfigPath = "/srv/docker_configs" # Base directory for configs on the remote server
$localConfigSourcePath = ".\config"       # Local directory for configs (relative to this script)
# --- End of modifications ---

Write-Host "Preparing to upload configuration files to $remoteUser@${remoteHost}:$remoteBaseConfigPath"

# 1. Attempt to create the remote directory via SSH
Write-Host "Attempting to create remote directory $remoteBaseConfigPath via SSH (if it doesn't exist)..."
try {
    # ssh command will prompt for password (unless SSH keys are configured)
    ssh "$($remoteUser)@$($remoteHost)" "mkdir -p $remoteBaseConfigPath"
    Write-Host "Remote directory check/creation command executed." -ForegroundColor Cyan
}
catch {
    Write-Warning "Failed to create remote directory via SSH. Error: $($_.Exception.Message)"
    Write-Warning "Please ensure the directory $remoteBaseConfigPath already exists on the remote server and that user $remoteUser has appropriate permissions."
}

# 2. Iterate and upload all files from the local config directory
Write-Host "Starting configuration file upload..."
try {
    $configFiles = Get-ChildItem -Path $localConfigSourcePath -File
    if ($configFiles.Count -eq 0) {
        Write-Warning "No files found in local configuration directory: $localConfigSourcePath"
    } else {
        foreach ($fileItem in $configFiles) {
            $localFilePath = $fileItem.FullName
            $remoteFileName = $fileItem.Name
            $remoteFilePath = "$remoteBaseConfigPath/$remoteFileName" # Linux-style path concatenation

            Write-Host "Uploading $localFilePath to $remoteUser@${remoteHost}:$remoteFilePath ..."
            try {
                # scp command will prompt for password (unless SSH keys are configured)
                scp $localFilePath "$($remoteUser)@$($remoteHost):$($remoteFilePath)"
                Write-Host "Successfully uploaded $remoteFileName." -ForegroundColor Green
            }
            catch {
                Write-Error "Failed to upload $remoteFileName. Error: $($_.Exception.Message)"
            }
        }
    }
    Write-Host "All configuration file upload operations completed." -ForegroundColor Green
}
catch {
    Write-Error "An error occurred during file iteration or upload. Error: $($_.Exception.Message)"
}